package com.yunji.web.monitor;

import com.yunji.common.annotation.Log;
import com.yunji.common.base.BaseController;
import com.yunji.common.base.response.Response;
import com.yunji.common.base.response.ResponseHelper;
import com.yunji.common.enums.common.BusinessType;
import com.yunji.common.model.domain.SysLogininforDO;
import com.yunji.common.utils.ExcelUtil;
import com.yunji.system.service.ISysLogininforService;
import com.yunji.system.service.SysPasswordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 系统访问记录
 *
 * @author ruoyi
 */
@Api(tags = "系统访问记录")
@RestController
@RequestMapping("/monitor/logininfor")
public class SysLogininforController extends BaseController {
    @Autowired
    private ISysLogininforService logininforService;

    @Autowired
    private SysPasswordService passwordService;

    @ApiOperation("查询系统登录日志集合")
    @PreAuthorize("@ss.hasPermi('monitor:logininfor:list')")
    @GetMapping("/list")
    public Response<List<SysLogininforDO>> list(SysLogininforDO logininfor) {
        List<SysLogininforDO> list = logininforService.selectLogininforList(logininfor);
        return ResponseHelper.successWithPage(list);
    }

    @ApiOperation("导出登录日志")
    @Log(title = "登录日志", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('monitor:logininfor:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysLogininforDO logininfor) {
        List<SysLogininforDO> list = logininforService.selectLogininforList(logininfor);
        ExcelUtil<SysLogininforDO> util = new ExcelUtil<>(SysLogininforDO.class);
        util.exportExcel(response, list, "登录日志");
    }

    @ApiOperation("批量删除系统登录日志")
    @PreAuthorize("@ss.hasPermi('monitor:logininfor:remove')")
    @Log(title = "登录日志", businessType = BusinessType.DELETE)
    @DeleteMapping("/{infoIds}")
    public Response<String> remove(@PathVariable Long[] infoIds) {
        Assert.isTrue(logininforService.deleteLogininforByIds(infoIds) > 0, "删除失败");
        return ResponseHelper.success();
    }

    @ApiOperation("清空系统登录日志")
    @PreAuthorize("@ss.hasPermi('monitor:logininfor:remove')")
    @Log(title = "登录日志", businessType = BusinessType.CLEAN)
    @DeleteMapping("/clean")
    public Response<String> clean() {
        logininforService.cleanLogininfor();
        return ResponseHelper.success();
    }

    @ApiOperation("账户解锁")
    @PreAuthorize("@ss.hasPermi('monitor:logininfor:unlock')")
    @Log(title = "账户解锁", businessType = BusinessType.OTHER)
    @GetMapping("/unlock/{userName}")
    public Response<String> unlock(@PathVariable("userName") String userName) {
        passwordService.clearLoginRecordCache(userName);
        return ResponseHelper.success();
    }
}
